package org.test.test1.controller;

import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.test.test1.Result.Result;
import org.test.test1.dao.pojo.Employee;
import org.test.test1.service.EmployeeMapperService;

@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    EmployeeMapperService employeeMapperService;

    @PostMapping("/searchDpEmployees/{currentPage}/{pageSize}")
    public Result searchDpEmployees(@PathVariable("currentPage") long currentPage,@PathVariable("pageSize") long pageSize){
        Result result = employeeMapperService.listEmployees(currentPage, pageSize);
        return result;
    }

    @PostMapping("/listEmployees")
    public Result listEmployees(){
        //Result result = employeeMapperService.listEmployees(currentPage, pageSize);
        Result result = employeeMapperService.findAll();
        return result;
    }

    @PostMapping("/addEmployee")
    public Result addEmployee(@RequestBody Employee employee){
        employeeMapperService.addEmployee(employee);
        Result result = employeeMapperService.findById(employee.getId());
        //if (result.getData()!=null){
        //    return Result.fail(400,"添加失败");
        //}
        result.setMsg("添加成功");
        return result;
    }

    @RequestMapping("/updateEmployee")
    public Result updateEmployee(@RequestBody Employee employee){
        employeeMapperService.updateEmployee(employee);
        Result result = employeeMapperService.findById(employee.getId());
        result.setMsg("修改成功！");
        return result;
    }
    @RequestMapping("/delEmployee/{id}")
    public Result delEmployee(@PathVariable("id") Integer id){
        employeeMapperService.delEmployee(id);
        Result result = employeeMapperService.findById(id);
        if (result.getData()!=null){
            return Result.fail(400,"删除失败");
        }
        result.setMsg("删除成功！");
        return result;
    }
}
